package com.fyp.client.ptma;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

import com.fyp.client.ptma.adapters.CustomAdapterLog;
import com.fyp.client.ptma.model.Application;
import com.fyp.client.ptma.model.ApplicationVersion;
import com.fyp.client.ptma.model.Defect;

public class LogScreen extends AbstractActivity
{

	private EditText tvVersionComments;
	private ListView lvDefectComment;
	private CustomAdapterLog adapterLog;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		adapterLog = new CustomAdapterLog(this);

		ApplicationVersion version = getIntent().getExtras().getParcelable(
				"dataToCarry");
		tvVersionComments.setText(version.getVersionComment());
		adapterLog.setData(version);

		lvDefectComment.setAdapter(adapterLog);

	}

	public void onClick(View view)
	{
		switch (view.getId())
		{
		case R.id.btnAddComment:
			btnAddComment();

		}
	}

	private static final int ADD_COMMENT_REQUEST = 1007;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);

		if (ADD_COMMENT_REQUEST == requestCode && resultCode == RESULT_OK)
		{
			Defect defect = data.getExtras().getParcelable("dataToReturn");
			if (defect != null)
			{
				adapterLog.getData().getDefects().add(defect);
				adapterLog.notifyDataSetChanged();
			}
		}
	}

	private void btnAddComment()
	{
		if (isLatestVersionInstalled(adapterLog.getData()))
		{

			CustomIntent intent = new CustomIntent(this,
					AddCommentDialogue.class);
			intent.putExtra("dataToCarry", adapterLog.getData());
			this.startActivityForResult(intent, ADD_COMMENT_REQUEST);

		} else
		{
			showAddCommentError();
		}
	}

	private void showAddCommentError()
	{
		AlertDialog.Builder alert;
		alert = new AlertDialog.Builder(this);

		alert.setTitle("Error");
		alert.setMessage("To add defect you have to install latest version of application. Please click Download to install latest Version");

		alert.setPositiveButton("Download",
				new DialogInterface.OnClickListener()
				{

					public void onClick(DialogInterface dialog, int whichButton)
					{
						LogScreen.this.finish();

					}
				});
		alert.setNeutralButton("Cancel", new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int whichButton)
			{

			}
		});

		alert.show();

	}

	private boolean isLatestVersionInstalled(ApplicationVersion appVersion)
	{
		PackageInfo pInfo = null;
		String ver = null;
		try
		{
			pInfo = getPackageManager().getPackageInfo(
					"com.speedsoftware.sqleditor", 0);
			ver = pInfo.versionName;
		} catch (NameNotFoundException e)
		{
			ver = "";
			System.out.println(this.getClass().getName() + ": "
					+ e.getMessage());

		}
		Application app = adapterLog.getData().getVersionPk().getApplication();
		String latestVer = app.getLatestVersion();
		if (BuildConfig.DEBUG)

			return true;

		return ver.equals(latestVer);
	}

	@Override
	int getContentView()
	{
		// TODO Auto-generated method stub
		return R.layout.log;
	}

	@Override
	void initViewRefereces()
	{
		tvVersionComments = (EditText) findViewById(R.id.tvVersionComments);
		lvDefectComment = (ListView) findViewById(R.id.lvDefectComment);

	}

}
